﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;

    public class BatchOrder : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnAddDetail;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnEncaseAddDetail;
        protected Button btnEncaseClose;
        protected Button btnEncaseDeleteDetail;
        protected Button btnEncaseNesting;
        protected Button btnEncasePrint;
        protected Button btnImportData;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnSave;
        protected Button btnSearchAndAddDetail;
        protected DropDownList ddlEncaseForm;
        protected DropDownList ddlEncaseTo;
        protected DropDownList ddlStatus;
        protected DropDownList DropDownList1;
        protected HtmlForm F7031;
        protected GridView gvBatchOrderDetail;
        protected GridView gvdShipmentContainer;
        protected HtmlInputHidden hdnContainerID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOrdersID;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlInputHidden hdnWaveID;
        protected HtmlInputHidden hiddenFlag;
        protected HtmlInputButton htmlbtnReport;
        protected HtmlInputImage Image1;
        protected Label lblBatchOrder;
        protected Label lblCreatedDate;
        protected Label lblDescr;
        protected Label lblMsg;
        protected Label lblOrderCategoryTotalQty;
        protected Label lblOrderSkuTotalQty;
        protected Label lblSCTotal;
        protected Label lblSCTotalQty;
        protected Label lblStatus;
        protected Label lblTotalHeight;
        protected Label lblTotalHeightText;
        protected Label lblTotalLength;
        protected Label lblTotalLengthText;
        protected Label lblTotalOrderQty;
        protected Label lblTotalValue;
        protected Label lblTotalValueText;
        protected Label lblTotalVolume;
        protected Label lblTotalVolumeText;
        protected Label lblTotalWeight;
        protected Label lblTotalWeightText;
        protected Label lblTotalWidth;
        protected Label lblTotalWidthText;
        protected Label lblWaveID;
        protected Label lblWaveType;
        protected LinkButton lnkbtnAllocation;
        protected LinkButton lnkbtnBatchOutboundList;
        protected LinkButton lnkbtnBatchShippingOrder;
        protected LinkButton lnkbtnCloseOrder;
        protected LinkButton lnkbtnConfirmPicked;
        protected LinkButton lnkbtnMergePrintPickList;
        protected LinkButton lnkbtnPreAllocation;
        protected LinkButton lnkbtnPrintBatchPickAdvice;
        protected LinkButton lnkbtnPrintPick;
        protected LinkButton lnkbtnPrintPickDivide;
        protected LinkButton lnkbtnReleasePickTasks;
        protected LinkButton lnkbtnRevertAllocation;
        protected LinkButton lnkbtnRevertPreAllocation;
        protected LinkButton lnkbtnShipping;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        private decimal SCTotalQty;
        protected HtmlTableCell tdlnkbtnAllocation;
        protected HtmlTableCell tdlnkbtnBatchOutboundList;
        protected HtmlTableCell tdlnkbtnBatchShippingOrder;
        protected HtmlTableCell tdlnkbtnCloseOrder;
        protected HtmlTableCell tdlnkbtnConfirmPicked;
        protected HtmlTableCell tdlnkbtnMergePrintPickList;
        protected HtmlTableCell tdlnkbtnPreAllocation;
        protected HtmlTableCell tdlnkbtnPrintBatchPickAdvice;
        protected HtmlTableCell tdlnkbtnPrintPick;
        protected HtmlTableCell tdlnkbtnPrintPickDivide;
        protected HtmlTableCell tdlnkbtnReleasePickTasks;
        protected HtmlTableCell tdlnkbtnRevertAllocation;
        protected HtmlTableCell tdlnkbtnRevertPreAllocation;
        protected HtmlTableCell tdlnkbtnShipping;
        private decimal TotalHeight;
        private decimal TotalLength;
        private decimal TotalValue;
        private decimal TotalVolume;
        private decimal TotalWeight;
        private decimal TotalWidth;
        protected TextBox txtCreatedDate;
        protected TextBox txtDescr;
        protected TextBox txtOrderCategoryTotalQty;
        protected TextBox txtOrderSkuTotalQty;
        protected TextBox txtTotalOrderQty;
        protected TextBox txtWaveID;
        protected UpdatePanel up1;

        protected void btnAddContainerDetail_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("EncaseMent.aspx?from={0}&WaveID={1}", this.hdnFrom.Value, this.txtWaveID.Text), true);
        }

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue.ToUpper() != "SHIPPED")
            {
                if (string.IsNullOrEmpty(this.hdnWaveID.Value) || (base.DataEntityViewState == null))
                {
                    try
                    {
                        this.InsertWaveInfo();
                        base.Response.Redirect(string.Format("BatchOrderDetail.aspx?from={0}&ID={1}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtWaveID.Text.Trim())), true);
                    }
                    catch (Exception ex)
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F7300_003") + ex.Message;
                    }
                }
                else
                {
                    base.Response.Redirect(string.Format("BatchOrderDetail.aspx?from={0}&ID={1}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtWaveID.Text.Trim())), true);
                }
            }
            else
            {
                this.lblMsg.Text = "该批量订单已经发货完成,不能再新增发货订单!";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = "状态不等于New无法删除!";
                }
                else
                {
                    List<string> waveId = new List<string> {
                        this.txtWaveID.Text
                    };
                    this.lblMsg.Text = OutboundServiceClientProxy.GetInstance().DeleteWave(waveId) + base.GetGlobalMsgResource("F7300_005");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
            }
        }

        protected void btnDeleteContainerDetail_Click(object sneder, EventArgs e)
        {
            List<string> ContainerIDs = base.GetSelectedIDsInGridView(this.gvdShipmentContainer, "ContainerID");
            List<string> TotalQty = base.GetSelectedIDsInGridView(this.gvdShipmentContainer, "TotalQty");
            for (int i = 0; TotalQty.Count > i; i++)
            {
                if (Convert.ToDecimal(TotalQty[i]) > 0M)
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F7600_010"), ContainerIDs[i]);
                    this.lblMsg.Visible = true;
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "ShippingPage", "<script language='javascript'>setTab('one',3,3);</script>", false);
                    return;
                }
            }
            List<string> InternalContainerIDs = base.GetSelectedIDsInGridView(this.gvdShipmentContainer, "InternalContainerID");
            string result = OutboundServiceClientProxy.GetInstance().DeleteShipmentContainer(InternalContainerIDs);
            this.lblMsg.Text = base.GetGlobalMsgResource("F7600_011");
            this.lblMsg.Visible = true;
            this.SetWaveInfoForm();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "ShippingPage", "<script language='javascript'>setTab('one',3,3);</script>", false);
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtWaveID.Text.Trim()) && (this.hdnFlag.Value == "1"))
            {
                try
                {
                    List<string> orderID = base.GetSelectedIDsInGridView(this.gvBatchOrderDetail, "OrderID");
                    List<string> status = base.GetSelectedIDsInGridView(this.gvBatchOrderDetail, "Status");
                    if ((status != null) && (status.Count > 0))
                    {
                        for (int i = 0; status.Count > i; i++)
                        {
                            if (status[i].ToUpper() != "NEW")
                            {
                                this.lblMsg.Text = "状态不等于新建无法删除！";
                                return;
                            }
                        }
                    }
                    if ((orderID == null) && (orderID.Count == 0))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F7300_004");
                    }
                    else
                    {
                        this.lblMsg.Text = OutboundDetailServiceClientProxy.GetInstance().DeleteWave(this.txtWaveID.Text, orderID) + base.GetGlobalMsgResource("F7300_005");
                        this.LoadData();
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
                }
                finally
                {
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3) </script>", false);
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_007");
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            this.SetWaveInfoForm();
            if (!string.IsNullOrEmpty(this.hdnRtnValue.Value))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one'," + this.hdnRtnValue.Value + ",3)</script>", false);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                this.UpdateWaveInfo();
            }
            else
            {
                this.InsertWaveInfo();
            }
        }

        protected void btnSearchAndAddDetail_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue.ToUpper() != "SHIPPED")
            {
                if (string.IsNullOrEmpty(this.hdnWaveID.Value) || (base.DataEntityViewState == null))
                {
                    try
                    {
                        this.InsertWaveInfo();
                        base.Response.Redirect(string.Format("BatchOrderDetail2.aspx?from={0}&ID={1}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtWaveID.Text.Trim())), true);
                    }
                    catch (Exception ex)
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F7300_003") + ex.Message;
                    }
                }
                else
                {
                    base.Response.Redirect(string.Format("BatchOrderDetail2.aspx?from={0}&ID={1}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtWaveID.Text.Trim())), true);
                }
            }
            else
            {
                this.lblMsg.Text = "该批量订单已经发货完成,不能再新增发货订单!";
            }
        }

        protected string CheckSelectedIDsStatusInGridView(GridView gv)
        {
            string ids = "";
            for (int i = 0; i < gv.Rows.Count; i++)
            {
                if (((CheckBox) gv.Rows[i].FindControl("chbSelect")).Checked && (gv.DataKeys[i]["Status"].ToString().ToUpper() != "NEW"))
                {
                    ids = ids + gv.DataKeys[i]["OrderID"].ToString() + ",";
                }
            }
            if (string.IsNullOrEmpty(ids))
            {
                return "";
            }
            return ids.Substring(0, ids.Length - 1);
        }

        protected void ddlEncaseForm_OnTextChanged(object sender, EventArgs e)
        {
            if (this.ddlEncaseForm.SelectedValue != "0")
            {
                string strContarnerID = this.ddlEncaseForm.SelectedValue;
                OrdersInfo orderInfo = base.DataEntityViewState as OrdersInfo;
                if (orderInfo.ShipmentContainerInfos.Count > 0)
                {
                    List<ShipmentContainerInfo> shipmentContainerList = orderInfo.ShipmentContainerInfos.FindAll(x => x.ContainerID != strContarnerID).FindAll(x => x.ParentContainerID != strContarnerID);
                    if (shipmentContainerList.Count > 0)
                    {
                        this.ddlEncaseTo.DataSource = shipmentContainerList;
                        this.ddlEncaseTo.DataTextField = "ContainerID";
                        this.ddlEncaseTo.DataValueField = "ContainerID";
                        this.ddlEncaseTo.DataBind();
                        this.ddlEncaseTo.Items.Insert(0, new ListItem("-----", "0"));
                        this.ddlEncaseTo.Items.Insert(this.ddlEncaseTo.Items.Count, new ListItem("清除嵌套", ""));
                    }
                }
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "ShippingPage", "<script language='javascript'>  document.getElementById('DivEncaseNesting').style.display=''; setTab('one',3,3); </script>", false);
        }

        protected void Encase_Click(object sender, EventArgs e)
        {
            if ((this.ddlEncaseTo.SelectedValue != "0") && (this.ddlEncaseForm.SelectedValue != "0"))
            {
                base.ShowMsgLabel(this.lblMsg, OutboundServiceClientProxy.GetInstance().EncaseNesting(this.ddlEncaseForm.SelectedValue, this.ddlEncaseTo.SelectedValue));
            }
            else
            {
                base.ShowMsgLabel(this.lblMsg, base.GetGlobalMsgResource("F7600_027"));
            }
            this.ddlEncaseTo.Items.Clear();
            this.LoadData();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "ShippingPage", "<script language='javascript'>  setTab('one',3,3); </script>", false);
        }

        private void EncaseNesting()
        {
            OrdersInfo orderInfo = base.DataEntityViewState as OrdersInfo;
            if (orderInfo.ShipmentContainerInfos.Count > 0)
            {
                this.ddlEncaseForm.DataSource = orderInfo.ShipmentContainerInfos;
                this.ddlEncaseForm.DataTextField = "ContainerID";
                this.ddlEncaseForm.DataValueField = "ContainerID";
                this.ddlEncaseForm.DataBind();
                this.ddlEncaseForm.Items.Insert(0, new ListItem("-----", "0"));
            }
        }

        protected void gvBatchOrderDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7031", this.gvBatchOrderDetail);
        }

        protected void gvBatchOrderDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as WaveInfo).WaveDetailInfo;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3) </script>", false);
        }

        protected void gvBatchOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvBatchOrderDetail.PageIndex * this.gvBatchOrderDetail.PageSize;
                e.Row.Cells[1].Text = ((e.Row.RowIndex + 1) + i).ToString();
                string strStatus = this.gvBatchOrderDetail.DataKeys[e.Row.RowIndex]["Status"].ToString();
                ((Label) e.Row.FindControl("lblOrderID")).Attributes.Add("onclick", "popOrder('" + ((Label) e.Row.FindControl("lblOrderID")).Text + "')");
                ((Label) e.Row.FindControl("lblOrderID")).Attributes.Add("style", "cursor:hand");
                switch (strStatus.ToUpper())
                {
                    case "NEW":
                        e.Row.Cells[3].BackColor = Color.FromName("#ebf6fe");
                        break;

                    case "PARTPRE-ALLOCATED":
                        e.Row.Cells[3].BackColor = Color.FromName("#dff1fd");
                        break;

                    case "PRE-ALLOCATED":
                        e.Row.Cells[3].BackColor = Color.FromName("#d2ebfd");
                        break;

                    case "PARTALLOCATED":
                        e.Row.Cells[3].BackColor = Color.FromName("#c1e3fc");
                        break;

                    case "ALLOCATED":
                        e.Row.Cells[3].BackColor = Color.FromName("#b2dcfb");
                        break;

                    case "RELEASED":
                        e.Row.Cells[3].BackColor = Color.FromName("#dffdf8");
                        break;

                    case "PARTPICKED":
                        e.Row.Cells[3].BackColor = Color.FromName("#c9fdf4");
                        break;

                    case "PICKED":
                        e.Row.Cells[3].BackColor = Color.FromName("#fbfddc");
                        break;

                    case "INLOADING":
                        e.Row.Cells[3].BackColor = Color.FromName("#fdecdc");
                        break;

                    case "LOADED":
                        e.Row.Cells[3].BackColor = Color.FromName("#fce1c8");
                        break;

                    case "PARTSHIPPED":
                        e.Row.Cells[3].BackColor = Color.FromName("#fdd8da");
                        break;

                    case "SHIPPED":
                        e.Row.Cells[3].BackColor = Color.FromName("#fccdd0");
                        break;

                    case "CANCELLED":
                        e.Row.Cells[3].BackColor = Color.FromName("#fde7d6");
                        break;

                    case "CLOSED":
                        e.Row.Cells[3].BackColor = Color.FromName("#c4f5cb");
                        break;

                    case "SCHEDULED":
                        e.Row.Cells[3].BackColor = Color.FromName("##91e09c");
                        break;
                }
            }
        }

        protected void gvBatchOrderDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                WaveInfo waveInfo = base.DataEntityViewState as WaveInfo;
                DataView dv = CADataConverter.ToDataTable(waveInfo.OrdersInfo).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvBatchOrderDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);</script>", false);
            }
        }

        protected void gvdShipmentContainer_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7061", this.gvdShipmentContainer);
        }

        protected void gvdShipmentContainer_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvdShipmentContainer.PageIndex * this.gvdShipmentContainer.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                if (((Label) e.Row.FindControl("lblQty")) != null)
                {
                    this.SCTotalQty += decimal.Parse(((Label) e.Row.FindControl("lblQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblWeight")) != null)
                {
                    this.TotalWeight += decimal.Parse(((Label) e.Row.FindControl("lblWeight")).Text);
                }
                if (((Label) e.Row.FindControl("lblVolume")) != null)
                {
                    this.TotalVolume += decimal.Parse(((Label) e.Row.FindControl("lblVolume")).Text);
                }
                if (((Label) e.Row.FindControl("lblValue")) != null)
                {
                    this.TotalValue += decimal.Parse(((Label) e.Row.FindControl("lblValue")).Text);
                }
                if (((Label) e.Row.FindControl("lblLength")) != null)
                {
                    this.TotalLength += decimal.Parse(((Label) e.Row.FindControl("lblLength")).Text);
                }
                if (((Label) e.Row.FindControl("lbl_Weight")) != null)
                {
                    this.TotalWidth += decimal.Parse(((Label) e.Row.FindControl("lbl_Weight")).Text);
                }
                if (((Label) e.Row.FindControl("lblHeight")) != null)
                {
                    this.TotalHeight += decimal.Parse(((Label) e.Row.FindControl("lblHeight")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblSCTotalQty.Text = base.GetPreferenceDigit(this.SCTotalQty);
                this.lblTotalWeightText.Text = base.GetPreferenceDigit(this.TotalWeight);
                this.lblTotalVolumeText.Text = base.GetPreferenceDigit(this.TotalVolume);
                this.lblTotalValueText.Text = base.GetPreferenceDigit(this.TotalValue);
                this.lblTotalLengthText.Text = base.GetPreferenceDigit(this.TotalLength);
                this.lblTotalWidthText.Text = base.GetPreferenceDigit(this.TotalWidth);
                this.lblTotalHeightText.Text = base.GetPreferenceDigit(this.TotalHeight);
            }
        }

        protected void gvdShipmentContainer_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                OrdersInfo ordersInfo = base.DataEntityViewState as OrdersInfo;
                DataView dv = CADataConverter.ToDataTable(ordersInfo.ShipmentContainerInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvdShipmentContainer, dv);
            }
            catch (Exception ex)
            {
                base.ShowMsgLabel(this.lblMsg, ex.Message);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "ShippingPage", "<script language='javascript'>setTab('one',3,3);</script>", false);
            }
        }

        private void HiddenMsg(Label lblMsg)
        {
            lblMsg.Visible = false;
        }

        private void InsertWaveInfo()
        {
            string strWaveId = string.Empty;
            WaveInfo waveInfo = new WaveInfo {
                WaveID = this.txtWaveID.Text,
                Status = this.ddlStatus.SelectedValue,
                Descr = this.txtDescr.Text,
                WaveType = "",
                PalletPickDispatchMethod = "",
                PiecePickDispatchMethod = "",
                LabelsPerCase = 0,
                LabelsPerPallet = 0,
                CasePickDispatchMethod = "",
                BatchFlag = "",
                AutoBatch = "",
                AllowConsolidation = "",
                ExternalWaveID = "",
                CreatedBy = base.UserInfo.UserID
            };
            try
            {
                strWaveId = OutboundServiceClientProxy.GetInstance().InsertWave(waveInfo);
                this.txtWaveID.Text = strWaveId;
                this.hdnWaveID.Value = strWaveId;
                this.lblMsg.Text = strWaveId + base.GetGlobalMsgResource("F7300_008");
                waveInfo.WaveID = strWaveId;
                base.DataEntityViewState = waveInfo;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnAllocation_Click(object sender, EventArgs e)
        {
            try
            {
                this.HiddenMsg(this.lblMsg);
                OutboundServiceClientProxy.GetInstance().BatchOrderActions(this.txtWaveID.Text.Trim(), "Allocation");
                this.SetWaveInfoForm();
                string result = string.Empty;
                for (int i = 0; this.gvBatchOrderDetail.Rows.Count > i; i++)
                {
                    if (((this.gvBatchOrderDetail.DataKeys[i]["Status"].ToString().ToUpper() != "NEW") && (this.gvBatchOrderDetail.DataKeys[i]["Status"].ToString().ToUpper() != "PRE-ALLOCATED")) && (this.gvBatchOrderDetail.DataKeys[i]["Status"].ToString().ToUpper() != "PARTPRE-ALLOCATED"))
                    {
                        string CS00001 = result;
                        result = CS00001 + this.gvBatchOrderDetail.Rows[i].Cells[2].Text + " " + ((Label) this.gvBatchOrderDetail.Rows[i].FindControl("lblStaus")).Text + ";";
                    }
                    else
                    {
                        result = result + this.gvBatchOrderDetail.Rows[i].Cells[2].Text + " 分配失败!;";
                    }
                }
                this.ShowMsg(this.lblMsg, result);
            }
            catch (Exception ex)
            {
                this.ShowMsg(this.lblMsg, ex.Message);
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnBatchOutboundList_Click(object sender, EventArgs e)
        {
            string strOrders = string.Empty;
            try
            {
                List<string> orderID = base.GetSelectedIDsInGridView(this.gvBatchOrderDetail, "OrderID");
                if ((orderID == null) || (orderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_015");
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
                    return;
                }
                for (int i = 0; orderID.Count > i; i++)
                {
                    strOrders = strOrders + "'" + orderID[i] + "',";
                }
                this.hdnOrdersID.Value = strOrders.Substring(0, strOrders.Length - 1);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3);popPrinBatchOutboundList(); </script>", false);
        }

        protected void lnkbtnBatchShippingOrder_Click(object sender, EventArgs e)
        {
            string strOrders = string.Empty;
            try
            {
                List<string> orderID = base.GetSelectedIDsInGridView(this.gvBatchOrderDetail, "OrderID");
                if ((orderID == null) || (orderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_015");
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
                    return;
                }
                for (int i = 0; orderID.Count > i; i++)
                {
                    strOrders = strOrders + "'" + orderID[i] + "',";
                }
                this.hdnOrdersID.Value = strOrders.Substring(0, strOrders.Length - 1);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3);popPrinBatchShippingOrder(); </script>", false);
        }

        protected void lnkbtnClosed_Click(object sender, EventArgs e)
        {
            string result = string.Empty;
            try
            {
                if (((this.ddlStatus.SelectedValue.ToUpper() == "NEW") || (this.ddlStatus.SelectedValue.ToUpper() == "PARTSHIPPED")) || (this.ddlStatus.SelectedValue.ToUpper() == "SHIPPED"))
                {
                    result = OutboundServiceClientProxy.GetInstance().BatchClosed(new List<string> { this.txtWaveID.Text });
                    this.LoadData();
                    this.lblMsg.Text = "单据 " + result + " 关闭成功!";
                }
                else
                {
                    this.lblMsg.Text = "状态不等于新建或者部分发货或者发货,无法关闭！";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnConfirmPicked_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gvBatchOrderDetail.Rows.Count > 0)
                {
                    OutboundServiceClientProxy.GetInstance().BatchConfirmPicked(this.txtWaveID.Text);
                    this.SetWaveInfoForm();
                    string result = string.Empty;
                    for (int i = 0; this.gvBatchOrderDetail.Rows.Count > i; i++)
                    {
                        string StrStatus = this.gvBatchOrderDetail.DataKeys[i]["Status"].ToString().ToUpper();
                        if ((StrStatus == "PICKED") || (StrStatus == "PARTPICKED"))
                        {
                            string CS00001 = result;
                            result = CS00001 + this.gvBatchOrderDetail.Rows[i].Cells[2].Text + " " + ((Label) this.gvBatchOrderDetail.Rows[i].FindControl("lblStaus")).Text + ";";
                        }
                        else if ((StrStatus == "PARTSHIPPED") || (StrStatus == "SHIPPED"))
                        {
                            result = result + this.gvBatchOrderDetail.Rows[i].Cells[2].Text + " 已经发货,无法拣货操作!;";
                        }
                        else
                        {
                            result = result + this.gvBatchOrderDetail.Rows[i].Cells[2].Text + " 拣货确认失败!;";
                        }
                        this.lblMsg.Text = result;
                    }
                }
                else
                {
                    this.lblMsg.Text = "没有找到要拣货的明细数据！";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnMergePrintPickList_Click(object sender, EventArgs e)
        {
            string strOrders = string.Empty;
            try
            {
                List<string> orderID = base.GetSelectedIDsInGridView(this.gvBatchOrderDetail, "OrderID");
                if ((orderID == null) || (orderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_015");
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
                    return;
                }
                for (int i = 0; orderID.Count > i; i++)
                {
                    strOrders = strOrders + "'" + orderID[i] + "',";
                }
                this.hdnOrdersID.Value = strOrders.Substring(0, strOrders.Length - 1);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3);popPrintMergePickList(); </script>", false);
        }

        protected void lnkbtnPreAllocation_Click(object sender, EventArgs e)
        {
            try
            {
                this.HiddenMsg(this.lblMsg);
                OutboundServiceClientProxy.GetInstance().BatchOrderActions(this.txtWaveID.Text.Trim(), "PreAllocation");
                this.SetWaveInfoForm();
                string result = string.Empty;
                for (int i = 0; this.gvBatchOrderDetail.Rows.Count > i; i++)
                {
                    if (this.gvBatchOrderDetail.DataKeys[i]["Status"].ToString().ToUpper() != "NEW")
                    {
                        string CS00001 = result;
                        result = CS00001 + this.gvBatchOrderDetail.Rows[i].Cells[2].Text + " " + ((Label) this.gvBatchOrderDetail.Rows[i].FindControl("lblStaus")).Text + ";";
                    }
                    else
                    {
                        result = result + this.gvBatchOrderDetail.Rows[i].Cells[2].Text + " 预分配失败!;";
                    }
                }
                this.ShowMsg(this.lblMsg, result);
            }
            catch (Exception ex)
            {
                this.ShowMsg(this.lblMsg, ex.Message);
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnPrintBatchPickAdvice_Click(object sender, EventArgs e)
        {
            string strOrders = string.Empty;
            try
            {
                string ids = this.CheckSelectedIDsStatusInGridView(this.gvBatchOrderDetail);
                if (!string.IsNullOrEmpty(ids))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_023") + ids;
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
                    return;
                }
                List<string> orderID = base.GetSelectedIDsInGridView(this.gvBatchOrderDetail, "OrderID");
                if ((orderID == null) || (orderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_015");
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
                    return;
                }
                for (int i = 0; orderID.Count > i; i++)
                {
                    strOrders = strOrders + orderID[i] + ",";
                }
                this.hdnOrdersID.Value = strOrders.Substring(0, strOrders.Length - 1);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3);popPrintBatchPickAdvice(); </script>", false);
        }

        protected void lnkbtnPrintPickDivide_Click(object sender, EventArgs e)
        {
            string strOrders = string.Empty;
            try
            {
                List<string> orderID = base.GetSelectedIDsInGridView(this.gvBatchOrderDetail, "OrderID");
                if ((orderID == null) || (orderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_015");
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
                    return;
                }
                for (int i = 0; orderID.Count > i; i++)
                {
                    strOrders = strOrders + "'" + orderID[i] + "',";
                }
                this.hdnOrdersID.Value = strOrders.Substring(0, strOrders.Length - 1);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3);popPrinPickDivide(); </script>", false);
        }

        protected void lnkbtnPrintPickList_Click(object sender, EventArgs e)
        {
            string strOrders = string.Empty;
            try
            {
                List<string> orderID = base.GetSelectedIDsInGridView(this.gvBatchOrderDetail, "OrderID");
                if ((orderID == null) || (orderID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7300_015");
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
                    return;
                }
                for (int i = 0; orderID.Count > i; i++)
                {
                    strOrders = strOrders + "'" + orderID[i] + "',";
                }
                this.hdnOrdersID.Value = strOrders.Substring(0, strOrders.Length - 1);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7300_006") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3); </script>", false);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3);popPrintPickList(); </script>", false);
        }

        protected void lnkbtnReleasePickTasks_Click(object sender, EventArgs e)
        {
            try
            {
                this.HiddenMsg(this.lblMsg);
                string result = OutboundServiceClientProxy.GetInstance().BatchOrderActions(this.txtWaveID.Text.Trim(), "ReleaseTasks");
                if (!string.IsNullOrEmpty(result))
                {
                    this.ShowMsg(this.lblMsg, result + base.GetGlobalMsgResource("F7300_012"));
                }
                this.SetWaveInfoForm();
            }
            catch (Exception ex)
            {
                this.ShowMsg(this.lblMsg, ex.Message);
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnRevertAllocation_Click(object sender, EventArgs e)
        {
            try
            {
                this.HiddenMsg(this.lblMsg);
                string result = OutboundServiceClientProxy.GetInstance().BatchOrderActions(this.txtWaveID.Text.Trim(), "RevertAllocation");
                if (!string.IsNullOrEmpty(result))
                {
                    this.ShowMsg(this.lblMsg, result + base.GetGlobalMsgResource("F7300_014"));
                }
                this.SetWaveInfoForm();
            }
            catch (Exception ex)
            {
                this.ShowMsg(this.lblMsg, ex.Message);
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnRevertPreAllocation_Click(object sender, EventArgs e)
        {
            try
            {
                this.HiddenMsg(this.lblMsg);
                string result = OutboundServiceClientProxy.GetInstance().BatchOrderActions(this.txtWaveID.Text.Trim(), "RevertPreAllocation");
                if (!string.IsNullOrEmpty(result))
                {
                    this.ShowMsg(this.lblMsg, result + base.GetGlobalMsgResource("F7300_013"));
                }
                this.SetWaveInfoForm();
            }
            catch (Exception ex)
            {
                this.ShowMsg(this.lblMsg, ex.Message);
                SysLog.GetInstance().LogError(ex);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnShipping_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gvBatchOrderDetail.Rows.Count > 0)
                {
                    OutboundServiceClientProxy.GetInstance().BatchShipping(this.txtWaveID.Text);
                    this.SetWaveInfoForm();
                    string result = string.Empty;
                    for (int i = 0; this.gvBatchOrderDetail.Rows.Count > i; i++)
                    {
                        string StrStatus = this.gvBatchOrderDetail.DataKeys[i]["Status"].ToString().ToUpper();
                        if ((StrStatus == "PARTSHIPPED") || (StrStatus == "SHIPPED"))
                        {
                            string CS00001 = result;
                            result = CS00001 + this.gvBatchOrderDetail.Rows[i].Cells[2].Text + " " + ((Label) this.gvBatchOrderDetail.Rows[i].FindControl("lblStaus")).Text + ";";
                        }
                        else
                        {
                            result = result + this.gvBatchOrderDetail.Rows[i].Cells[2].Text + " 发货确认失败!;";
                        }
                    }
                    this.lblMsg.Text = result;
                }
                else
                {
                    this.lblMsg.Text = "没有找到要发货的明细数据！";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);$.unblockUI();</script>", false);
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "WaveStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.txtWaveID.Text = base.Request.QueryString["ID"].ToString();
                this.hdnWaveID.Value = this.txtWaveID.Text;
                this.txtWaveID.Enabled = false;
                this.SetWaveInfoForm();
                this.hdnFlag.Value = "1";
            }
            else
            {
                this.txtWaveID.Text = "";
                this.txtDescr.Text = "";
                this.hdnWaveID.Value = "";
                this.hdnFlag.Value = "0";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);</script>", false);
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["Tab"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one'," + base.Request.QueryString["Tab"] + ",3);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetWaveInfoForm()
        {
            try
            {
                string waveID = this.txtWaveID.Text.Trim();
                if (!string.IsNullOrEmpty(waveID))
                {
                    WaveInfo waveInfo = OutboundServiceClientProxy.GetInstance().GetWaveByID(waveID);
                    base.DataEntityViewState = waveInfo;
                    if (waveInfo != null)
                    {
                        this.txtWaveID.Text = waveInfo.WaveID;
                        this.ddlStatus.SelectedValue = waveInfo.Status;
                        this.txtCreatedDate.Text = base.GetPreferenceShortDate(waveInfo.CreatedDate);
                        if ((waveInfo.Status.ToUpper() == "CLOSED") || (waveInfo.Status.ToUpper() == "SHIPPED"))
                        {
                            this.btnAddDetail.Enabled = false;
                            this.btnSearchAndAddDetail.Enabled = false;
                            this.btnDeleteDetail.Enabled = false;
                        }
                        else if (waveInfo.Status.ToUpper() == "PARTSHIPPED")
                        {
                            this.btnSearchAndAddDetail.Enabled = false;
                        }
                        this.txtDescr.Text = waveInfo.Descr;
                        base.BindData2GridView(this.gvBatchOrderDetail, waveInfo.OrdersInfo);
                        base.BindData2GridView(this.gvdShipmentContainer, waveInfo.ShipmentContainerInfos);
                        this.EncaseNesting();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void ShowMsg(Label lblMsg, string msg)
        {
            lblMsg.Visible = true;
            lblMsg.Text = msg;
        }

        private void UpdateWaveInfo()
        {
            WaveInfo waveInfo = new WaveInfo();
            waveInfo.WaveID = this.txtWaveID.Text;
            waveInfo.Descr = this.txtDescr.Text;
            waveInfo.Status = this.ddlStatus.SelectedValue;
            waveInfo.Descr = this.txtDescr.Text;
            waveInfo.WaveType = "";
            waveInfo.PalletPickDispatchMethod = "";
            waveInfo.PiecePickDispatchMethod = "";
            waveInfo.LabelsPerCase = 0;
            waveInfo.LabelsPerPallet = 0;
            waveInfo.CasePickDispatchMethod = "";
            waveInfo.BatchFlag = "";
            waveInfo.AutoBatch = "";
            waveInfo.AllowConsolidation = "";
            waveInfo.ExternalWaveID = "";
            waveInfo.CreatedBy = "";
            waveInfo.UpdatedBy = base.UserInfo.UserID;
            try
            {
                this.lblMsg.Text = OutboundServiceClientProxy.GetInstance().UpdateWave(waveInfo) + base.GetGlobalMsgResource("F7300_009");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

